%%Two Players -- Missile and Target

function ex_MissileTarget
warning off
missile_strategies={'Tail Chasing (Pursuit Guidance)','Constant Bearing','Parallel Navigation (CATD)','Beamer Rider','Propotional Navigation (PN)','Trajectory Learning'}
missile_strategy=menu('Please choose strategy of Missile:',missile_strategies)
target_strategies={'Straight Line','Circle Motion','Spiral Motion','Random Simple Motion','Random Walk','Anti-Missile'}
target_strategy=menu('Please choose strategy of Target:',target_strategies)

ax=[-5000,5000,-5000,5000];
figure(1);clf;winontop(1);
axis manual;axis(ax);
hold on;grid on;
title('Missile is at the origin. Where is the target?');
M_img=newimage('images/missile.png');
plot(0,0);
drawimage(M_img,[0 0]);
[T_x,T_y,b]=ginput(1);
if b==3,
    T_x=2500;
    T_y=0;
end;

M_pos=[0,0];M_angle=0;M_speed=400;
T_pos=[T_x,T_y];T_angle=pi/2;T_speed=300;
T_ang_speed=-0.1;

Missile=newplayer('pos',M_pos,'ang',M_angle,'speed',M_speed,'motion','step_func','legend','r','image','images/missile.png','step_func','MT_missile_step','strategy',1)
Missile.strategy=missile_strategy
Missile.PNG_constant=4;
Missile.T_trace=[];
Missile.max_learn_step=10;
Missile.max_predict_step=100;
Missile.measure_noise_magnitude=0.1;

Target=newplayer('pos',T_pos,'ang',T_angle,'speed',T_speed,'ang_speed',T_ang_speed,'motion','step_func','legend','b','image','images/plane.bmp','step_func','MT_target_step','strategy',1')
Target.strategy=target_strategy

G=newgame({Target,Missile},'max_time',20,'max_step',10000,'h',0.1,'axis',ax,'pause','pause(0.0001);','min_r',10,'before_draw_func','MT_show_info','draw_game_trace_func','MT_draw_game_trace')
G=simgame(G)
winontop(1,0)



k=menu('Do you want to try all strategies in batch mode under the same settings?',{'Wah, Good!','No, thanks!'});
if k==1, 
    G.missile_strategies=missile_strategies;
    G.target_strategies=target_strategies;
    G.T_pos=T_pos;
    G.T_ang=T_angle;
    G.T_ang_speed=T_ang_speed;
    G.pause='';
    batch_test(G);
end;

function res=batch_test(G)
m=length(G.target_strategies);
n=length(G.missile_strategies);
for k_target=1:m,
    res={};
    res{1}={'Missile Strategy','Interception Time','Simulation Steps','Miss Distance'};
    for k_missile=1:n,
        [t,step,miss]=test_pair(G,k_target,k_missile);
        res{k_missile+1}={G.missile_strategies{k_missile},t,step,miss};
    end;
    fn=['../../fig-MADS/MT-res-',num2str(k_target),'.tex'];
    gen_latex_table(fn,res);
end;


function [t,step,miss]=test_pair(G,k_target,k_missile)
    rand('seed',100);
    disp(['Missile using strategy:',G.missile_strategies{k_missile}])
    disp(['Target using strategy:',G.target_strategies{k_target}])
    G2=G;
    G2.P{1}.strategy=k_target;
    G2.P{2}.strategy=k_missile;
    G2.P{1}.pos=G.T_pos;
    G2.P{1}.ang=G.T_ang;
    G2.P{1}.ang_speed=G.T_ang_speed;
    G2.P{2}.pos=[0 0];
    G2.P{2}.ang=0;
    G2.draw=1;
    G2.figure_file=['../../fig-MADS/MT-',num2str(k_target),num2str(k_missile),'.jpg'];
    G2.t=0;
    G2.step=0;
    G2.r=inf;
    G2=simgame(G2);
    disp(['Time:',num2str(G2.t),' Steps:',num2str(G2.step),' Miss:',num2str(G2.r)]);
    t=G2.t;
    step=G2.step;
    miss=G2.r;
    % pause;
    